package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.ICONConst;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    MenuService menuService;

    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Menu> menuList = menuService.findAll();
            return new Result(true,"查询所有菜单",menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询所有菜单失败");
        }
    }

    @RequestMapping("/find_role_menu")
    public Result find_role_menu(Integer id){
        try {
            Integer[] integers = menuService.find_role_menu(id);
            return new Result(true,"查询该角色已有菜单成功",integers);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,"查询该角色已有菜单失败");
        }
    }

    @RequestMapping("/getAllOnemenu")
    public Result getAllOnemenu(){

        try {
            List<Menu> menuList = menuService.getAllOnemenu();
            return new Result(true, MessageConst.GET_MENU_SUCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.GET_MENU_FAIL);
        }

    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu){
        try {
            menuService.edit(menu);
            return new Result(true, "编辑菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑菜单失败");
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        try {
           menuService.add(menu);
            return new Result(true, "新增菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增菜单失败");
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Menu menu = menuService.findById(id);
            return new Result(true, "查询菜单项成功",menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询菜单项失败");
        }
    }

    @RequestMapping("/delById")
    public Result delById(Integer id){
        try {
            menuService.delById(id);
            return new Result(true,"菜单删除成功");
        }catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"菜单删除失败");
        }
    }


    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = menuService.findPage(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/icons")
    public Result icons(){
        return new Result(true,"", ICONConst.getAllIcons()) ;
    }

    @RequestMapping("/getmenu")
    public Result getmenu(String username){

        try {
            List<Menu> menuList =  menuService.getMenu(username);

            return new Result(true, MessageConst.GET_MENU_SUCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.GET_MENU_FAIL);
        }

    }
}
